package algorithm.leetcode.array;

import java.util.HashMap;
import java.util.Map;

/**
 * 最长无重复字符的子串
 */
public class LongestSubString {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int left = 0;
        int maxLen = 0;
        for (int i=0; i<s.length(); i++) {
            if(map.containsKey(s.charAt(i))) {
                left = Math.max(left, map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i), i);
            maxLen = Math.max(maxLen, i-left+1);
        }
        return maxLen;
    }

    public static void main(String[] args) {
        new LongestSubString().lengthOfLongestSubstring("abcabcbb");
    }

}
